/**
 * 
 */
package io.gitee.falllee.dao.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;

import io.gitee.falllee.dao.util.FilterPlus;

/**
 * @author batty
 *
 */
public abstract class BaseDaoConfig implements EnvironmentAware {

    private String configName = "defaultDaoConfig";

    private String dbType = "mysql";

    private String enableFlag;

    private String enableFlagValue;

    private String createUser;

    private String createDate;

    private String updateUser;

    private String updateDate;
    
    private String headerParamEmpNo = "P-EmpNo";
    
    private String headerParamKey;

    private String keyType = "2";

    private Integer enableCache = 0;

    private Integer enableKafka = 0;

    private String topic;
    
    private Environment env;

    public String getConfigName() {
        return configName;
    }

    public void setConfigName(String configName) {
        this.configName = configName;
    }

    public String getDbType() {
        return dbType;
    }

    public void setDbType(String dbType) {
        this.dbType = dbType;
    }

    public String getEnableFlag() {
        return enableFlag;
    }

    public void setEnableFlag(String enableFlag) {
        this.enableFlag = enableFlag;
    }

    public String getEnableFlagValue() {
        return enableFlagValue;
    }

    public void setEnableFlagValue(String enableFlagValue) {
        this.enableFlagValue = enableFlagValue;
    }

    public String getCreateUser() {
        return createUser;
    }

    public void setCreateUser(String createUser) {
        this.createUser = createUser;
    }

    public String getCreateDate() {
        return createDate;
    }

    public void setCreateDate(String createDate) {
        this.createDate = createDate;
    }

    public String getUpdateUser() {
        return updateUser;
    }

    public void setUpdateUser(String updateUser) {
        this.updateUser = updateUser;
    }

    public String getUpdateDate() {
        return updateDate;
    }

    public void setUpdateDate(String updateDate) {
        this.updateDate = updateDate;
    }

    public String getHeaderParamEmpNo() {
        return headerParamEmpNo;
    }

    public void setHeaderParamEmpNo(String headerParamEmpNo) {
        this.headerParamEmpNo = headerParamEmpNo;
    }

    public String getHeaderParamKey() {
        return headerParamKey;
    }

    public void setHeaderParamKey(String headerParamKey) {
        this.headerParamKey = headerParamKey;
    }

    public String getKeyType() {
        return keyType;
    }

    public void setKeyType(String keyType) {
        this.keyType = keyType;
    }

    public String getValue(String val) {
        switch (val) {
            case "${enableFlag}":
                return getEnableFlag();
            case "${createUser}":
                return getCreateUser();
            case "${createDate}":
                return getCreateDate();
            case "${updateUser}":
                return getUpdateUser();
            case "${updateDate}":
                return getUpdateDate();
            default:
                return val;
        }
    }

    /**
     * 获取组件级配置字段，包含有效标记、创建人、创建时间、更新人、更新时间
     * 
     * @return
     */
    public List<String> getComponentConfigFields() {
        List<String> list = new ArrayList<>();
        if (StringUtils.hasLength(getEnableFlag())) {
            list.add(getEnableFlag().toLowerCase());
        }
        if (StringUtils.hasLength(getCreateUser())) {
            list.add(getCreateUser().toLowerCase());
        }
        if (StringUtils.hasLength(getCreateDate())) {
            list.add(getCreateDate().toLowerCase());
        }
        if (StringUtils.hasLength(getUpdateUser())) {
            list.add(getUpdateUser().toLowerCase());
        }
        if (StringUtils.hasLength(getUpdateDate())) {
            list.add(getUpdateDate().toLowerCase());
        }
        return list;
    }

    public Integer getEnableCache() {
        return enableCache;
    }

    public void setEnableCache(Integer enableCache) {
        this.enableCache = enableCache;
    }

    public Integer getEnableKafka() {
        return enableKafka;
    }

    public void setEnableKafka(Integer enableKafka) {
        this.enableKafka = enableKafka;
    }

    public String getTopic() {
        return topic;
    }

    public void setTopic(String topic) {
        this.topic = topic;
    }

    /**
     * 获取当前用户
     * 
     * @return
     */
    public abstract Object getCurrentUser();
    
    /**
     * 获取请求头
     * 
     * @return
     */
    public abstract Map<String, String> getHeaderParamsMap();
    
    /**
     * 增加全局条件
     * 
     * @param filterPlus
     */
    public abstract FilterPlus addGlobalFilter(FilterPlus filterPlus);

    @Override
    public void setEnvironment(final Environment environment) {
        this.env = environment;
    }

    public Environment getEnvironment() {
        return this.env;
    }
    
    public String getQuotes() {
        switch (dbType) {
        case DaoConst.MYSQL:
            return DaoConst.QUOTES_MYSQL;
        case DaoConst.MSSQL:
            return DaoConst.QUOTES_MSSQL;
        case DaoConst.ORACLE:
            return DaoConst.QUOTES_ORACLE;
        case DaoConst.POSTGRE_SQL:
            return DaoConst.QUOTES_POSTGRE_SQL;
        default:
            return DaoConst.QUOTES_OTHER;
        }
    }
}
